package com.qianzhou.eduUserBoot.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianzhou.eduCommon.dto.UserDTO;
import com.qianzhou.eduCommon.dto.UserRoleDTO;
import com.qianzhou.eduCommon.mapper.RolesMapper;
import com.qianzhou.eduCommon.mapper.UserMapper;
import com.qianzhou.eduCommon.mapper.UserRoleRelationMapper;
import com.qianzhou.eduCommon.po.Roles;
import com.qianzhou.eduCommon.po.User;
import com.qianzhou.eduCommon.po.UserRoleRelation;
import com.qianzhou.eduCommon.utils.PageInfoQuery;
import com.qianzhou.eduCommon.vo.PageInfoVO;
import com.qianzhou.eduCommon.vo.RoleVO;
import com.qianzhou.eduCommon.vo.UserVO;
import com.qianzhou.eduUserBoot.converter.RoleConverter;
import com.qianzhou.eduUserBoot.converter.UserConverter;
import com.qianzhou.eduUserBoot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: qian.zhou
 * @Description: 用户service实现类
 * @Date Created in 2021-08-31-21:23
 * @Modified By:
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RolesMapper rolesMapper;
    @Autowired
    private UserRoleRelationMapper userRoleRelationMapper;

    @Override
    public User getOne(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public PageInfoVO<User> findAllUserByPage(UserDTO dto) {
        if (dto.getPageNum() != null && dto.getPageNum() != null) {
            return null;
        }
        PageInfoQuery<User> pageInfoQuery = new PageInfoQuery<>();
        IPage<User> page = new Page<User>(dto.getPageNum(), dto.getPageSize());
        /*QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del",dto.getIsDel());*/
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getIsDel, dto.getIsDel());
        lambdaQueryWrapper.eq(User::getName, dto.getName());
        if (dto.getStartTime() != null) {
            lambdaQueryWrapper.ge(User::getCreateTime, dto.getStartTime());
        }
        if (dto.getEndTime() != null) {
            lambdaQueryWrapper.le(User::getCreateTime, dto.getStartTime());
        }
        userMapper.selectPage(page, lambdaQueryWrapper);
        return pageInfoQuery.getPageInfoData(page);
    }

    @Override
    public Integer updateUserStatus(Integer id, String status) {
        /**
         * 更新用户的状态信息
         */
        User user = new User();
        user.setId(id);
        user.setStatus(status);
        return userMapper.updateById(user);
    }

    @Override
    public UserVO login(UserDTO dto) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getName, dto.getName());
        lambdaQueryWrapper.eq(User::getPassword, dto.getPassword());
        User user = userMapper.selectOne(lambdaQueryWrapper);
        if (user == null) {
            return null;
        }
        return UserConverter.INSTANCE.poConvertVo(user);
    }

    @Override
    public List<RoleVO> findUserRoleById(Integer id) {
        List<Roles> roles = userMapper.findUserRoleById(id);
        /**
         * converter转换器将对应的roles转换为对应的roleVo
         */
        return RoleConverter.INSTANCE.poConvertVo(roles);
    }

    @Override
    public Integer userContextRole(UserRoleDTO dto) {
        /**
         * 根据用户的id获取对应的值
         */
        User user = userMapper.selectById(dto.getUserId());
        /**
         * 1.根据用户id删除对应的用户和role之间的关系
         */
        LambdaQueryWrapper<UserRoleRelation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserRoleRelation::getUserId, dto.getUserId());
        userRoleRelationMapper.delete(lambdaQueryWrapper);
        /**
         * 2.然后插入对应的id和角色的信息
         */
        int len = dto.getRoleIdList().size();
        int num = 0;
        for (int i = 0; i < len; i++) {
            UserRoleRelation userRoleRelation = new UserRoleRelation();
            userRoleRelation.setUserId(dto.getUserId());
            userRoleRelation.setRoleId(dto.getRoleIdList().get(i));
            userRoleRelation.setCreatedBy(user.getName());
            userRoleRelation.setUpdatedBy(user.getName());
            Date date = new Date();
            userRoleRelation.setCreatedTime(date);
            userRoleRelation.setUpdatedTime(date);
            userRoleRelationMapper.insert(userRoleRelation);
            num++;
        }
        return num;
    }

    /**
     * 根据用户的id获取到对应的权限信息，
     * @param user_id
     * @return
     */
    @Override
    public Map<Object,Object> getUserPermissions(Integer user_id) {
        /**
         * 1.获取当前用户对应的角色id
         */

        /**
         * 2.根据角色id获取到对应的菜单id
         */

        /**
         * 3.根据角色id获取到对应的资源id
         */
        Map<Object,Object> map = new HashMap<>();
        return map;
    }
}
